

#import "MemberCardRecordsViewController.h"
#import "sxview.h"
#import "HAODEsingleton.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "addhuiyuanViewController.h"
#import "RecordsCell.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"
#import "UIButton+WebCache.h"
#import "huiyuanDetailsViewController.h"
#import "GetProductListViewController.h"
#import "TheLoginViewController.h"
#import "shouyingViewController.h"
#import "MemberCardTopUpViewController.h"
#import "BusinessGatheringDetailViewController.h"
@interface MemberCardRecordsViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UIDatePicker* da;

@end

@implementation MemberCardRecordsViewController

{
    UISearchBar *search;
    HAODEsingleton* commdity;
    MJRefreshHeaderView* _header;
    MJRefreshFooterView* _footer;
    NSMutableArray*orderNo;
    NSMutableArray*orderid;
    NSMutableArray*orderType;
    NSMutableArray*payAmount;
    NSMutableArray*gtype;
    NSMutableArray*tradeNo;
    NSMutableArray*status;
    NSMutableArray* remark;
    NSMutableArray* createDate;
    NSMutableArray* image;
    NSString *result;
    int pageNumber;
    BOOL saveAccount;
    
}
@synthesize alt = alt;

-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    
    self.title=@"储值记录";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    
   
    
    if ([commdity.gengxin isEqualToString:@""]) {
       [self add];
        commdity.gengxin=@"123";
    }else{
    
    
    
    }
    
    
}

-(void)add:(UIButton*)sender{
    MemberCardTopUpViewController *detailViewController = [[MemberCardTopUpViewController alloc] init];
    [self.navigationController pushViewController:detailViewController animated:YES];

    
    
    
}


- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    [self addFooter];
    [self addheader];
    commdity=[HAODEsingleton shared];
    
    
    
    commdity=[HAODEsingleton shared];
    commdity.opentime=nil;
    commdity.closeTime=nil;
    commdity.content3=@"";
    //table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) ];
    _table.delegate = self;
    _table.dataSource = self;
    _table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_table];
    
    
    
    
 
    
}
-(void)add{
    
    pageNumber = 1;
    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:commdity.memberId forKey:@"memberId"];
    [param setValue:@"20" forKey:@"pageSize"];
    [param setValue:[NSString stringWithFormat:@"%d" ,pageNumber] forKey:@"pageIndex"];
    //NSLog(@"====%@",param);
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Business/Member/CardRecords" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,90/2)];
                view.backgroundColor= zuiqianhuicolor;
                _table.tableHeaderView=view;
                
                
                UILabel*jifen=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 70, 45)];
                jifen.text=@"当前余额";
                jifen.font=[UIFont systemFontOfSize:15];
                [view addSubview:jifen];
                UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 45-0.35, ScreenWidth, 0.35)];
                lineLabel1.backgroundColor=qianhuicolor;
                [view addSubview:lineLabel1];
                
           
                NSString*GoldPrice=[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"cardMoney"]];
                
//                NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
//                [numberFormatter setPositiveFormat:@"###,##0;"];
//                NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
                UILabel*jifens=[[UILabel alloc]initWithFrame:CGRectMake(jifen.right, 0, 180, 45)];
                jifens.text=[NSString stringWithFormat:@"%@ 元",GoldPrice];
                jifens.textColor=qianheicolor;
                jifens.font=[UIFont systemFontOfSize:15];
                
                
                
                NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@ 元",  GoldPrice]];
                
                [AttributedStr addAttribute:NSFontAttributeName
                 
                                      value:[UIFont systemFontOfSize:20]
                 
                                      range:NSMakeRange(0, [GoldPrice length])];
                
                [AttributedStr addAttribute:NSForegroundColorAttributeName
                 
                                      value:[UIColor blackColor]
                 
                                      range:NSMakeRange(0, [GoldPrice length])];
               jifens.attributedText = AttributedStr;

                [view addSubview:jifens];

                
                UIButton*chongzhi=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-136/2-26/2, 6.5, 136/2, 33)];
                chongzhi.backgroundColor=luseColor;
                [chongzhi setTitle:@"充值" forState:UIControlStateNormal];
                [chongzhi setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                chongzhi .titleLabel.font=[UIFont systemFontOfSize:conten14fot];
                [chongzhi addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
                [chongzhi.layer setCornerRadius:2.0]; //设置矩圆角半径
                [chongzhi.layer setBorderWidth:0.3];   //边框宽度
                CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
                CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){23/255.0, 148/255.0, 152/255.0, 1.0});
                [chongzhi.layer setBorderColor:tureBTncolorref];//边框颜色

                [view addSubview:chongzhi];
                
                
                
                orderid= [[NSMutableArray alloc] init];
                orderType= [[NSMutableArray alloc] init];
                payAmount= [[NSMutableArray alloc] init];
            
                status= [[NSMutableArray alloc] init];
               
                createDate = [[NSMutableArray alloc] init];
                gtype= [[NSMutableArray alloc] init];
                
                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                for (NSDictionary* dicc in arrr )
                {
                  
                    [orderid addObject:[dicc valueForKey:@"orderId"]];
                    [payAmount addObject:[dicc valueForKey:@"amount"]];
                    [status addObject:[dicc valueForKey:@"status"]];
                    
                    [createDate addObject:[dicc valueForKey:@"createDate"]];
                    [gtype addObject:[dicc valueForKey:@"gtype"]];
                    
                    
                    
                }
                [_table reloadData];
                
            }else{
                
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    TheLoginViewController *detailViewController = [[TheLoginViewController alloc] init];
                    [self.navigationController pushViewController:detailViewController animated:YES];
                }
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
    
    
    
    
}





//上拉刷新
- (void)addFooter
{
    
    
    
    MJRefreshFooterView* footer = [MJRefreshFooterView footer];
    footer.scrollView = _table;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView* refreshView)
    {
        
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        if (Token ==nil) {
            Token=@"dd";
        }
        NSMutableDictionary *param = [NSMutableDictionary new];
        [param setValue:Token forKey:@"tokenId"];
        [param setValue:commdity.memberId forKey:@"memberId"];
        [param setValue:@"20" forKey:@"pageSize"];
        [param setValue:[NSString stringWithFormat:@"%d" ,pageNumber] forKey:@"pageIndex"];
        //NSLog(@"====%@",param);
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        [HttpRequest requestData:@"Business/Member/CardRecords" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                NSLog(@"====%@",severDataDict.dataDict);
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    
                    
                    
                    
                    NSMutableArray* arrr=[[NSMutableArray alloc]init];
                    arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                    for (NSDictionary* dicc in arrr )
                    {
                        [orderid addObject:[dicc valueForKey:@"orderId"]];
                        [payAmount addObject:[dicc valueForKey:@"amount"]];
                        [status addObject:[dicc valueForKey:@"status"]];
                        
                        [createDate addObject:[dicc valueForKey:@"createDate"]];
                        [gtype addObject:[dicc valueForKey:@"gtype"]];
                    }
                    [_table reloadData];
                    
                }else{
                    
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        
                        TheLoginViewController *detailViewController = [[TheLoginViewController alloc] init];
                        [self.navigationController pushViewController:detailViewController animated:YES];
                    }
                }
                
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
            
        }];
        
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:2.0f];
        NSLog(@"%@--->正在刷新", refreshView.class);
    };
    
}
- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    [_table reloadData];
    [refreshView endRefreshing];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
   
    return [gtype count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    static NSString *CellIdentifier = @"Cell";
    
    RecordsCell* cell = (RecordsCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"RecordsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    
    if (tableView == _table) {
        UIButton*jiantou = [[UIButton alloc]init];
        jiantou.frame = CGRectMake(0,0, 8, 13);
        [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
        
        //    cell.accessoryView = jiantou;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if ([[gtype objectAtIndex:indexPath.row]isEqualToString:@"1"]) {
        
         cell.gtype.text=[NSString stringWithFormat:@"%@",  @"储值卡充值"];
        
        }
        if ([[gtype objectAtIndex:indexPath.row]isEqualToString:@"2"]) {
            
            
            cell.gtype.text=[NSString stringWithFormat:@"%@",  @"购买商品"];
        }
       
        
        cell.gtype.textColor=qianheicolor;
        
        cell.createDate.textColor=shenhuicolor;
        
        NSRange range={0,16};
        NSString* string =[[NSString stringWithFormat:@"%@",[createDate objectAtIndex:indexPath.row]] substringWithRange:range];
        cell.createDate.text =[NSString stringWithFormat:@"%@",string];
        
        
        
        NSMutableAttributedString *AttributedStr1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",string]];
        
        [AttributedStr1 addAttribute:NSFontAttributeName
         
                              value:[UIFont systemFontOfSize:12]
         
                              range:NSMakeRange(0, 10)];
        
        [AttributedStr1 addAttribute:NSForegroundColorAttributeName
         
                              value:zhonghuiColor
         
                              range:NSMakeRange(0, 10)];
        cell.createDate.attributedText = AttributedStr1;
        
       
        
        
        
        
        
        NSString*GoldPrice=[NSString stringWithFormat:@"%@",[payAmount objectAtIndex:indexPath.row]];
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setPositiveFormat:@"###,##0;"];
        NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:GoldPrice.floatValue]];
        //    cell.payAmount.frame=CGRectMake(ScreenWidth-200, 15, 180, 16);
        //    cell.payAmount.textAlignment=NSTextAlignmentRight;
        cell.amount.text =[NSString stringWithFormat:@"￥%@",  formattedNumberString];
        cell.amount.font=[UIFont systemFontOfSize:18];
        cell.amount.textColor=qianheicolor;
        NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%@",  formattedNumberString]];
        
        [AttributedStr addAttribute:NSFontAttributeName
         
                              value:[UIFont systemFontOfSize:14.0]
         
                              range:NSMakeRange(0, 1)];
        
        [AttributedStr addAttribute:NSForegroundColorAttributeName
         
                              value:qianheicolor
         
                              range:NSMakeRange(0, 1)];
        cell.amount.attributedText = AttributedStr;
        
        //    cell.status.frame=CGRectMake(ScreenWidth-200, 35, 180, 16);
        //    cell.status.textAlignment=NSTextAlignmentRight;
        if ([[status objectAtIndex:indexPath.row]isEqualToString:@"Y"]) {
            cell.status.text =[NSString stringWithFormat:@"%@",@"交易成功"];
            cell.status.textColor=shenhuicolor;
            
            
            
        }else{
            cell.status.text =[NSString stringWithFormat:@"%@",@"交易失败"];
            
            cell.status.textColor=hongsecolor;
        }
        
        
        
        
        
        
        if (orderType.count !=indexPath.row+1) {
            UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(15, 80-0.35, ScreenWidth-30, 0.35)];
            lineLabel1.backgroundColor=qianhuicolor;
            [cell.contentView addSubview:lineLabel1];
            
        }
        
        if (orderType.count ==indexPath.row+1) {
            UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 80-0.35, ScreenWidth, 0.35)];
            lineLabel1.backgroundColor=qianhuicolor;
            [cell.contentView addSubview:lineLabel1];
            
        }
    
    
    
    }
    return cell;
}



- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return  80;
    
}




#pragma mark - Table view delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
}


//清空购物车
-(void)empty:(UIButton*)sender{
    
    
    UIAlertView*alt1 = [[UIAlertView alloc] initWithTitle:@"是否清空购物车！" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    [alt1 show];
    
    
}



- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

//上拉刷新
- (void)addheader
{
    MJRefreshHeaderView* footer = [MJRefreshHeaderView header];
    footer.scrollView = _table;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView* refreshView)
    {
        
        //        [Global hideProgressHUDInternal:mainScroll];
       
        [self add];
        //这里的RefreshView就是footer
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:3.0f];
        //        NSLog(@"%@--->正在刷新", mainScroll.class);
    };
    
}

@end
